<template>
  <div>
    <el-card style="width: 50%">
      <el-form :model="user" label-width="80px" style="padding-right: 20px">

        <div style="margin: 15px;text-align: center" v-loading="avatarLoad">
          <el-upload
              class="avatar-uploader"
              :action="this.$apiUrl+'/file/upload'"
              :headers="{token: user.token}"
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>


        <el-form-item label="账号" prop="">
          <el-input v-model="user.username" placeholder="你还没有登录哦，请登录后再试！" disabled></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="">
          <el-input v-model="user.name" placeholder="姓名"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="">
          <el-input v-model="user.phone" placeholder="电话"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="">
          <el-input v-model="user.email" placeholder="邮箱"></el-input>
        </el-form-item>

        <el-form-item label="头像" prop="">
          <el-input v-model="user.avatar" placeholder="头像图片地址填入这里"></el-input>
        </el-form-item>

        <el-form-item label="地址" prop="">
          <el-input type="textarea" v-model="user.address" placeholder="地址"></el-input>
        </el-form-item>

        <div style="text-align: center;margin-bottom: 20px">
          <el-button type="primary" @click="update" :loading="saveLoad">保存</el-button>
        </div>

      </el-form>

    </el-card>
  </div>
</template>

<script>
export default {
  name: "Person",
  data(){
    return {
      user: JSON.parse(localStorage.getItem('userInfo') || '{}'),
      avatarLoad: false,
      saveLoad: false
    }
  },
  created(){
  },
  methods: {
    update(){
      this.saveLoad = true
      //更新数据库和本地缓存
      this.$request.put('/user/update',this.user).then(res => {
        if (res.code === '200'){
          this.$message.success("保存成功")
          //更新本地缓存信息，要存字符串
          localStorage.setItem('userInfo',JSON.stringify(this.user))
          //更新后新头像通过子传父的方式通信给manager外框架去更新
          this.$emit('update:user',this.user)
        }else {
          this.$message.error("保存失败："+res.msg)
        }
        this.saveLoad = false
      })
    },
    handleAvatarSuccess(res,file,fileList){
      this.avatarLoad = false;
      if (res.code==="200"){
        //更新新头像链接
        this.user.avatar = res.data.fileUrl;
      }else {
        this.$message.error(res.msg)
        if (res.code === "401"){
          this.$router.push('/login')
        }
      }
    },
    beforeAvatarUpload(file){
      this.avatarLoad = true
      const fileType = file.type;
      const isJPG = fileType === 'image/jpeg' || fileType === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 20;
      if (!isJPG){
        this.$message.error('头像图片暂时只支持jpg/jpeg/png格式！')
        this.avatarLoad = false
      }
      if (!isLt2M){
        this.$message.error('头像图片文件大小不要超过20MB！')
        this.avatarLoad = false
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped>
/*deep穿透*/
/deep/.el-form-item__label {
  font-weight: bold;
}
/deep/.el-upload {
  bottom: 50%;
}
/*虚线 圆形*/
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}
.avatar{
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}
</style>